class AntiCSRFFilters {

  def TokenBuilderService tokenBuilderService;

  def filters = {
    all(controller: '*', action: '*') {
      before = {
        def toReturn = true
        if (request.method.equals("POST")) {
           tokenBuilderService.tokens.keySet().each {key -> log.debug(key)
            def value = tokenBuilderService.tokens[key]
            log.debug(value)
            if (params[key]) {
              if (params[key].equals(value.value)){
                 def expmillis = new Date().getTime();
                log.debug(expmillis +" " + value.expiration)
                toReturn = tokenBuilderService.checkToken(tokenBuilderService.tokens[key])
              }
            }
          }
          return toReturn;
        }
        else {
          return toReturn;
        }
      }
      after = {
        tokenBuilderService.pruneExpiredToken()
      }
      afterView = {

      }
    }
  }

}
